package com.example.lanchat.ui.TCP;

import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.lanchat.MainActivity;
import com.example.lanchat.R;
import com.example.lanchat.databinding.FragmentHomeBinding;
import com.example.lanchat.databinding.TcpFragmentBinding;
import com.example.lanchat.ui.home.HomeViewModel;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class TCPFragment extends Fragment {

    private TCViewModel mViewModel;
    private TcpFragmentBinding binding;
    MainActivity activity;
    public static TCPFragment newInstance() {
        return new TCPFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this).get(TCViewModel.class);
        binding = TcpFragmentBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        activity=(MainActivity) getActivity();
        RadioGroup radioGroup=binding.radio1;
        EditText InPutIP=binding.InPutIP;
        TextView IPAddress=binding.IPAddress;
        Button stareServer=binding.button3;
        Button stareConnect=binding.button4;
        IPAddress.setVisibility(View.INVISIBLE);
        stareServer.setVisibility(View.INVISIBLE);
        InPutIP.setVisibility(View.VISIBLE);
        stareConnect.setVisibility(View.VISIBLE);
        IPAddress.setText(getIP(getContext()));
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.button:
                        //客户端输入服务器ip链接服务器按钮
                        InPutIP.setVisibility(View.VISIBLE);
                        stareConnect.setVisibility(View.VISIBLE);
                        IPAddress.setVisibility(View.INVISIBLE);
                        stareServer.setVisibility(View.INVISIBLE);
                        activity.getSupportActionBar().setTitle("客户端");
                        activity.isTCPServer=true;
                        break;
                    case R.id.button2:
                        //服务器端显示本机IP和开服务器按钮
                        InPutIP.setVisibility(View.INVISIBLE);
                        stareConnect.setVisibility(View.INVISIBLE);
                        IPAddress.setVisibility(View.VISIBLE);
                        stareServer.setVisibility(View.VISIBLE);
                        activity.getSupportActionBar().setTitle("服务端");
                        activity.isTCPServer=false;
                        break;
                }
            }
        });
        stareServer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.startServer();
            }
        });
        stareConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String serverIP = InPutIP.getText().toString();
                activity.startClient(serverIP);
            }
        });
        return root;
    }
    public String getIP(Context context){

        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
                {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address))
                    {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
        catch (SocketException ex){
            ex.printStackTrace();
        }
        return null;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(TCViewModel.class);
        // TODO: Use the ViewModel
    }

}